<html>
<head>
<link rel="shortcut icon" href="./favicon.ico">
<link rel="stylesheet" type="text/css" href="./style.css">
<link rel="canonical" href="./lcm_function.html">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Takes in two 32-bit integers and uses the GCD-based algorithm to calculate their Least Common Multiple (LCM).  *This is not meant for synthesis!*">
<title>lcm function</title>
</head>
<body>

<p class="inline bordered"><b><a href="./lcm_function.vh">Source</a></b></p>
<p class="inline bordered"><b><a href="./legal.html">License</a></b></p>
<p class="inline bordered"><b><a href="./index.html">Index</a></b></p>

<h1>Least Common Multiple (LCM) Function</h1>
<p>Takes in two 32-bit integers and uses the GCD-based algorithm to calculate
 their Least Common Multiple (LCM).  <em>This is not meant for synthesis!</em></p>
<p>Reference: <a href="https://en.wikipedia.org/wiki/Least_common_multiple">Wikipedia: Least Common Multiple</a></p>
<p>Bring in the function at the start of the body of your module like so:</p>
<pre><code>`include "lcm_function.vh"
</code></pre>
<p>Pass the function values which, at elaboration time, are either constant or
 expressions which evaluates to constants. Then use the output value as an
 integer for a localparam, genvar, etc...</p>
<p>The formula is decomposed this way so the division always returns an
 integer, and so we can't end up with intermediate values that don't fit in an
 integer.</p>
<p>Since this is an included file, it must be idempotent. (defined only once globally)</p>

<pre>
`ifndef LCM_FUNCTION
`define LCM_FUNCTION

`include "<a href="./gcd_function.html">gcd_function</a>.vh"
`include "<a href="./abs_function.html">abs_function</a>.vh"

function integer lcm;
    input integer A;
    input integer B;
    begin
        lcm = abs(A) * (abs(B) / gcd(A,B));
    end
endfunction

`endif
</pre>

<hr>
<p><a href="./index.html">Back to FPGA Design Elements</a>
<center><a href="https://fpgacpu.ca/">fpgacpu.ca</a></center>
</body>
</html>

